// 获取应用实例

const app = getApp()
console.log("登录页面")

Page({

  /**
   * 页面的初始数据
   */
  globaldata: {
    nickName : "",
    avatarUrl : "",
    motto:'Log In',
    img_question:"../resources/question.svg",
    hasUserInfo: false,
    openid : "1",
    session : " ",
  },

  // 事件监听函数
 
  //跳转到手机注册
  bindRegister: function(){
    console.log("准备跳转"),
    wx.navigateTo({
      url:'../register/register'
    })
  },
  onShow: function (options) {
    var that=this;
    that.queryCartList();
    
    },
    queryCartList:function(){
    console.log('ok')
    },

  login:function(){
    var that = this;
    wx.switchTab({
      url: '/pages/home/home',
    });
    wx.login({
      success (res) {
        if (res.code) {
          //发起网络请求
          var appId = 'wx733459a12ad24a4b';
          var secret = '642c3272f7fbc47a3a31300db2a2593b';
          var code = res.code;
         // console.log(res.code);
          wx.request({
            url: 'https://api.weixin.qq.com/sns/jscode2session?appid=' + appId + '&secret=' + secret + '&js_code=' + code + '&grant_type=authorization_code',
            // url: 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='+appId+'&secret='+ secret +'&code='+code+'&grant_type=authorization_code',
            data: {    
            },
            header: {
              'content-type': 'json'
            },
            success:(res1)=> {
              console.log(res1)
              var openid = res1.data.openid; //返回openid
              var session = res1.data.session_key;
              wx.setStorageSync('openid', openid)
              wx.setStorageSync('session', session)
           }
         })
        } else {
          console.log('登录失败！' + res.errMsg)
        }
      }
    })
    // this.setData({
    //   openid:openid
    // })
    console.log()
    //调用微信api接口，获取登录人信息
    wx.getUserProfile({
      desc: '获取用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        const openid = wx.getStorageSync('openid')
        const session = wx.getStorageSync('session')
        console.log('openid ',openid)
        wx.request({
          url: 'http://localhost:8080/user',
          method:"POST",
          dataType:"json",
          data:{    
            user_name: res.userInfo.nickName,
            user_icon: res.userInfo.avatarUrl,
            openid: openid,
            session: session
          },
          success(res) {
            // console.log(res)
          },
          fail(err) {
            console.log(err)
          }
        })
        console.log("获取用户信息成功", res)
        this.setData({//添加及更新UI
          avatarUrl: res.userInfo.avatarUrl,
          nickName:res.userInfo.nickName,
        })
        wx.setStorage({
          key: 'userinfo',
          data:res.userInfo
        })
      },
      fail: res => {
        console.log("获取用户信息失败", res)
      }
    })
  },

  onLoad() {
    if (wx.getUserProfile) {
      this.setData({
        canIUseGetUserProfile: true
      })
    }
  },

  // onShow() {
  //   var self = this;
  //   wx.setStorage({
  //     key: 'userinfo',
  //     data:res.userInfo
  //   })
  // }

})